package com.base.monitor.model.enums.biz;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 蓝天消防主机预设报警码
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum BrightSkyFireControlMainEngineCodeEnum {

    CUE("00", 16, "联络信号"),
    FILE_ALARM("01", 16, "火警"),
    SUPERVISE("02", 16, "监管"),
    START("83", 16, "启动"),
    RESPONSE("43", 16, "应答"),
    EMERGENCY_STOP("23", 16, "急停"),
    MALFUNCTION("04", 16, "故障"),
    FILE_ALARM_CANCEL("05", 16, "火警取消"),
    SUPERVISE_CANCEL("06", 16, "监管取消"),
    STOP("87", 16, "模块关闭"),
    MODULE_RESPONSE_CANCEL("47", 16, "模块应答取消"),
    MODULE_SCRAM_CANCEL("27", 16, "模块急停取消"),
    BREAKDOWN_CANCEL("08", 16, "故障取消"),
    ON_SITE_INSPECTION_CRT("FE", 16, "巡检CRT"),
    UNDER_REPORTING("12", 16, "低报"),
    HIGHER_REPORTING("22", 16, "高报"),
    PHASE_LOSS_1("144", 10, "缺相1"),
    PHASE_STAGGER_1("145", 10, "错相1"),
    OVER_VOLTAGE_1("146", 10, "过压1"),
    UNDER_VOLTAGE_1("147", 10, "欠压1"),
    PHASE_LOSS_2("148", 10, "缺相2"),
    PHASE_STAGGER_2("149", 10, "错相2"),
    OVER_VOLTAGE_2("150", 10, "过压2"),
    UNDER_VOLTAGE_2("151", 10, "欠压2"),
    OVER_LOAD_1("152", 10, "过载1"),
    OVER_LOAD_2("153", 10, "过载2"),
    POWER_SUPPLY_INTERRUPTION_1("154", 10, "供电中断1"),
    POWER_SUPPLY_INTERRUPTION_2("155", 10, "供电中断2"),
    PHASE_LOSS_1_RECOVERY("156", 10, "缺相1恢复"),
    PHASE_STAGGER_1_RECOVERY("157", 10, "错相1恢复"),
    OVER_VOLTAGE_1_RECOVERY("158", 10, "过压1恢复"),
    UNDER_VOLTAGE_1_RECOVERY("159", 10, "欠压1恢复"),
    PHASE_LOSS_2_RECOVERY("160", 10, "缺相2恢复"),
    PHASE_STAGGER_2_RECOVERY("161", 10, "错相2恢复"),
    OVER_VOLTAGE_2_RECOVERY("162", 10, "过压2恢复"),
    UNDER_VOLTAGE_2_RECOVERY("163", 10, "欠压2恢复"),
    OVER_LOAD_1_RECOVERY("164", 10, "过载1恢复"),
    OVER_LOAD_2_RECOVERY("165", 10, "过载2恢复"),
    POWER_SUPPLY_INTERRUPTION_1_RECOVERY("166", 10, "供电中断1恢复"),
    POWER_SUPPLY_INTERRUPTION_2_RECOVERY("167", 10, "供电中断2恢复"),
    ;

    private String code;
    private Integer scale;
    private String message;

    /**
     * 根据进制查询符合条件的枚举值
     * @param scale 进制
     * @return
     */
    private static List<BrightSkyFireControlMainEngineCodeEnum> getEnumsByScale(int scale) {
        List<BrightSkyFireControlMainEngineCodeEnum> enums = new ArrayList<>(Arrays.asList(values()));
        return enums.stream().filter(item -> item.scale == scale).collect(Collectors.toList());
    }

    /**
     * 从指定进制中查找code符合枚举值
     * @param code 代码
     * @param scale 进制
     * @return
     */
    private static String getMessageFromCodeAndScale(String code, int scale) {
        List<BrightSkyFireControlMainEngineCodeEnum> enumsByScale = getEnumsByScale(scale);
        if (CollectionUtil.isEmpty(enumsByScale)) {
            return "";
        }
        List<BrightSkyFireControlMainEngineCodeEnum> collect = enumsByScale.stream()
                .filter(item -> item.code.equals(code))
                .collect(Collectors.toList());
        if (CollectionUtil.isEmpty(collect)) {
            return "";
        }
        return collect.get(0).message;
    }

    /**
     * 根据代码获取它的提示信息
     * @param code 代码
     * @return
     */
    public static String getMessageFromCode(String code) {
        String message = getMessageFromCodeAndScale(code, 10);
        if (StrUtil.isBlank(message)) {
            message = getMessageFromCodeAndScale(String.format("%02X", Integer.parseInt(code)), 16);
        }
        return message;
    }
}
